package com.platform.web.controller.base;

import java.util.List;
import com.platform.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.platform.common.annotation.Log;
import com.platform.common.enums.BusinessType;
import com.platform.base.domain.BaseFactory;
import com.platform.base.service.IBaseFactoryService;
import com.platform.common.core.controller.BaseController;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.utils.poi.ExcelUtil;
import com.platform.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 厂商信息Controller
 * 
 * @author wangqi
 * @date 2020-09-24
 */
@Controller
@RequestMapping("/base/factory")
public class BaseFactoryController extends BaseController
{
    private String prefix = "base/factory";

    @Autowired
    private IBaseFactoryService baseFactoryService;

    @RequiresPermissions("base:factory:view")
    @GetMapping()
    public String factory()
    {
        return prefix + "/factory";
    }

    /**
     * 查询厂商信息列表
     */
    @RequiresPermissions("base:factory:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BaseFactory baseFactory)
    {
        startPage();
        List<BaseFactory> list = baseFactoryService.selectBaseFactoryList(baseFactory);
        return getDataTable(list);
    }

    /**
     * @Author 王琦
     * @Description //厂商信息导入
     * @Date 2020/6/19 19:56
     * @Param [file, updateSupport]
     * @return com.platform.common.core.domain.AjaxResult
     */
    @Log(title = "厂商信息", businessType = BusinessType.IMPORT)
    @RequiresPermissions("base:factory:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<BaseFactory> util = new ExcelUtil<BaseFactory>(BaseFactory.class);
        List<BaseFactory> factoryList = util.importExcel(file.getInputStream());
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = baseFactoryService.importFactory(factoryList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * @Author 王琦
     * @Description //导入模板
     * @Date 2020/6/19 19:56
     * @Param []
     * @return com.platform.common.core.domain.AjaxResult
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<BaseFactory> util = new ExcelUtil<BaseFactory>(BaseFactory.class);
        return util.importTemplateExcel("厂商信息");
    }

    /**
     * 导出厂商信息列表
     */
    @RequiresPermissions("base:factory:export")
    @Log(title = "厂商信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BaseFactory baseFactory)
    {
        List<BaseFactory> list = baseFactoryService.selectBaseFactoryList(baseFactory);
        ExcelUtil<BaseFactory> util = new ExcelUtil<BaseFactory>(BaseFactory.class);
        return util.exportExcel(list, "厂商信息");
    }

    /**
     * 新增厂商信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存厂商信息
     */
    @RequiresPermissions("base:factory:add")
    @Log(title = "厂商信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated BaseFactory baseFactory)
    {
        baseFactory.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(baseFactoryService.insertBaseFactory(baseFactory));
    }

    /**
     * 修改厂商信息
     */
    @GetMapping("/edit/{factoryId}")
    public String edit(@PathVariable("factoryId") Long factoryId, ModelMap mmap)
    {
        BaseFactory baseFactory = baseFactoryService.getById(factoryId);
        mmap.put("baseFactory", baseFactory);
        return prefix + "/edit";
    }

    /**
     * 修改保存厂商信息
     */
    @RequiresPermissions("base:factory:edit")
    @Log(title = "厂商信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated BaseFactory baseFactory)
    {
        baseFactory.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(baseFactoryService.updateBaseFactory(baseFactory));
    }

    /**
     * 删除厂商信息
     */
    @RequiresPermissions("base:factory:remove")
    @Log(title = "厂商信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(baseFactoryService.deleteBaseFactoryByIds(ids));
    }
}
